package com.uenpay.im.ui.old.activity.company;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qyx.android.message.type.PacketType;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.RoundImageView;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-4-1 下午5:56:47
 * @copyRight http://www.qiyunxin.com
 * @function 选择部门成员
 */
public class ChooseEmployeeAdapter extends BaseAdapter {
	private ArrayList<EmployeeEntity> mArrayList = null;
	private LayoutInflater mLayoutInflater = null;
	private boolean isRadio;

	public ChooseEmployeeAdapter(Context _Context, boolean _isRadio,
			ArrayList<EmployeeEntity> _ArrayList) {
		isRadio = _isRadio;
		mLayoutInflater = LayoutInflater.from(_Context);
		mArrayList = _ArrayList;
	}

	@Override
	public int getCount() {
		return mArrayList == null ? 0 : mArrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mArrayList == null ? null : mArrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		EmployeeViewHodler mEmployeeViewHodler = null;
		final EmployeeEntity mEmployeeEntity = mArrayList.get(arg0);
		if (arg1 == null) {
			arg1 = mLayoutInflater.inflate(
					R.layout.item_choose_employee_layout, arg2, false);
			mEmployeeViewHodler = new EmployeeViewHodler();
			mEmployeeViewHodler.nameTv = (TextView) arg1
					.findViewById(R.id.name_tv);
			mEmployeeViewHodler.position_tv = (TextView) arg1
					.findViewById(R.id.position_tv);
			mEmployeeViewHodler.avatarIv = (RoundImageView) arg1
					.findViewById(R.id.avatar_iv);
			mEmployeeViewHodler.selected_iv = (ImageView) arg1
					.findViewById(R.id.selected_iv);
			arg1.setTag(mEmployeeViewHodler);
		} else {
			mEmployeeViewHodler = (EmployeeViewHodler) arg1.getTag();
		}
		mEmployeeViewHodler.nameTv.setText(mEmployeeEntity.nickname);
		mEmployeeViewHodler.position_tv.setText(mEmployeeEntity.position);
		mEmployeeViewHodler.avatarIv
				.SetUrl(APIConfiguration.getAvatarUrlNormal(
						mEmployeeEntity.custid, PacketType.PERSONAL));

		mEmployeeViewHodler.selected_iv.setVisibility(View.VISIBLE);
		if (mEmployeeEntity.is_selected == 1) {
			mEmployeeViewHodler.selected_iv
					.setImageResource(R.drawable.icon_checked);
		} else {
			mEmployeeViewHodler.selected_iv
					.setImageResource(R.drawable.icon_uncheck);
		}
		return arg1;
	}

	private class EmployeeViewHodler {
		private TextView nameTv, position_tv;
		private RoundImageView avatarIv;
		private ImageView selected_iv;
	}

	public void setSelected(String custId) {
		if (mArrayList != null) {

			if (isRadio) {
				for (int i = 0, size = mArrayList.size(); i < size; i++) {
					if (mArrayList.get(i).custid.equals(custId)) {
						mArrayList.get(i).is_selected = 1;
					}else{
						mArrayList.get(i).is_selected = 0;
					}
				}
			}else{
				for (int i = 0, size = mArrayList.size(); i < size; i++) {
					if (mArrayList.get(i).custid.equals(custId)) {
						if (mArrayList.get(i).is_selected == 1) {
							mArrayList.get(i).is_selected = 0;
						} else {
							mArrayList.get(i).is_selected = 1;
						}
					}
				}
			}
			notifyDataSetChanged();
		}
	}

	public ArrayList<EmployeeEntity> getSelectedUser() {
		ArrayList<EmployeeEntity> arrayList = new ArrayList<EmployeeEntity>();
		for (int i = 0, size = mArrayList.size(); i < size; i++) {
			if (mArrayList.get(i).is_selected == 1) {
				arrayList.add(mArrayList.get(i));
			}
		}
		return arrayList;
	}

}
